package fxc.example;

import tech.tablesaw.api.DoubleColumn;
import tech.tablesaw.api.Table;
import tech.tablesaw.plotly.Plot;
import tech.tablesaw.plotly.components.*;
import tech.tablesaw.plotly.traces.BarTrace;
import tech.tablesaw.plotly.traces.HistogramTrace;

import java.util.Arrays;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        //例子1： y = 15*x1 + 4.025*x2 + 6*x3+10 + 2
        //x1 N(74,0.01)
        //x2 N(60,0.5)
        //x3 poisson(3)
        Model model1 = new Model();
        model1.addNormalVariable(74, 0.01, 15);
        model1.addNormalVariable(60, 0.5, 4.025);
        model1.addPoissonVariable(3,  6);
        model1.residual = 2;
        MonteCarloSimulation monteCarloSimulation1 = new MonteCarloSimulation();
        monteCarloSimulation1.setModel(model1);
        monteCarloSimulation1.simulation(5000);
        Map<String, Double> statMap = monteCarloSimulation1.getStatMap();
        for (Map.Entry<String, Double> stat:statMap.entrySet()){
            System.out.println(stat.getKey()+": "+stat.getValue());
        }

        double[] dependentVariables = monteCarloSimulation1.getDependentVariables();

        DoubleColumn dc = DoubleColumn.create("Result", dependentVariables);
        Table dataTable = Table.create("Monte Carlo Simulation").addColumns(dc);
//        System.out.println(dataTable);
        dataTable.write().csv("1-5000.csv");
//        Table dataTable = Table.read().csv("dataTable.csv");
//        monteCarloSimulation1.setShowChart(true);
//        monteCarloSimulation1.createHistogramChart();


        //例子2： y = 3*x1 + 8*x2
        //x1 N(8,0.5)
        //x3 poisson(3)
        Model model2 = new Model();
        model2.addNormalVariable(8, 0.5, 3);
        model2.addPoissonVariable(3,  8);
        MonteCarloSimulation monteCarloSimulation2 = new MonteCarloSimulation();
        monteCarloSimulation2.setModel(model2);
        monteCarloSimulation2.simulation(new boolean[]{true, false},new double[]{6.5, 4},5000);
        Map<String, Double> statMap2 = monteCarloSimulation2.getStatMap();
        for (Map.Entry<String, Double> stat:statMap2.entrySet()){
            System.out.println(stat.getKey()+": "+stat.getValue());
        }

        double[] dependentVariables2 = monteCarloSimulation1.getDependentVariables();

        DoubleColumn dc2 = DoubleColumn.create("Result", dependentVariables);
        Table dataTable2 = Table.create("Monte Carlo Simulation").addColumns(dc2);
//        System.out.println(dataTable2);
        dataTable2.write().csv("2-10000.csv");
//        Table dataTable = Table.read().csv("dataTable.csv");
        monteCarloSimulation2.setShowChart(true);
        monteCarloSimulation2.createHistogramChart(500);
    }



}